package alan.springboot2x.swagger.knife4j.service;

import alan.springboot2x.swagger.knife4j.bean.Result;
import alan.springboot2x.swagger.knife4j.config.CustomerBlockHandler;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MyTestService {

    /**
     * SentinelResource中blockHandler等配置得生效，其得用在service层，在controll层是不会生效的
     * @return
     */
    @SentinelResource(value = "SentineTest",blockHandler = "handleException")
    public Result getStr(){
        return Result.setSuccess("body");
    }

    /**
     * 限流后使用CustomerBlockHandler类下的handleException方法
     * @return
     */
    @SentinelResource(value = "SentineTest2",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handleException")
    public Result getStr2(){
        return Result.setSuccess("body");
    }

    /**
     *
     * @return
     */
    @SentinelResource(value = "SentineTest3")
    public Result getStr3() {
        return Result.setSuccess("body");
    }

    public static Result handleException(BlockException exe){
        log.error("----->>>>>CustomerBlockHandler handleException exe:{} ",exe);
        return Result.setFail(exe.getMessage());
    }


}
